<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController 
{

    //我的主页
    public function my()
    {
    	$id = I('get.id');
     	//登陆信息显示
        $this->_userinfo($id); 

        //最近的提问
        $sql = 'select * from question where uid='. $id . ' order by create_time desc';
        $zuijinquestion = M()->query($sql);	
        $this->assign('zuijinquestion',$zuijinquestion);

        //最近的回答
        $sql = 'select t1.*,t2.title from answer t1 inner join question t2 on t1.qid=t2.id where t1.uid='. $id . ' order by create_time desc';
        $zuijinanswer = M()->query($sql);	
        $this->assign('zuijinanswer',$zuijinanswer);

        $this->display();
    }

    //我的用户中心
    public function myuser()
    {
    	//登陆信息显示
        $this->_userinfo($_SESSION['id']);
        //邮箱检测
        
        //自己发帖数量
        $sql = 'select count(*) as num from question where uid='.$_SESSION['id'];
        $countnum = M()->query($sql);
        $this->assign('countnum',current($countnum));


        //自己发帖显示
        $sql = 'select * from question where uid='.$_SESSION['id'];
        $myquestion = M()->query($sql);
        $this->assign('myquestion',$myquestion);

        $this->display();
    }

    //我的基本设置
    public function myset()
    {
    	//登陆信息显示
        $this->_userinfo($_SESSION['id']);


	    $this->display();
    }
    //myset资料
    public function edit()
    {
    	//print_r($_POST);
    	if (IS_POST) {
	    	if(!M('user')->create())
	    	{
				$arr = array(
				"error" => 0,
				"info" => "更新失败"
				);
				echo json_encode($arr);
				exit;
	    	}

			//邮箱验证
			$data = M('user')->where(array('id'=>$_SESSION['id']))->select();
			$data = current($data);
			
			$userinfo = M('user')->select();
			foreach ($userinfo as $v) 
			{
				if ($v["email"]!=$data['email'] && $v["email"]==$_POST['email']) 
				{
					$arr = array(
					"error" => 0,
					"info" => "邮箱已存在"
					);
					echo json_encode($arr);
					exit;
				}

				//昵称 username
				if ($v["username"]!=$data['username'] && $v['username']==$_POST['username']) {
					$arr = array(
					"error" => 0,
					"info" => "昵称已存在"
					);
					echo json_encode($arr);
					exit;
				}
			}
			
	        //入库

			$res = M('user')->where(array('id'=>$_SESSION['id']))->save(); 
			//昵称username 同步更新
			$userinfo = M('user')->where(array('id'=>$_SESSION['id']))->find();
			$_SESSION['username'] = $userinfo['username'];
			
			if ($res) {
				$arr = array(
				"error" => 1,
				"info" => "修改成功"
	        	);
	        	echo json_encode($arr);
			}else {
				$arr = array(
				"error" => 1,
				"info" => "修改失败"
	        	);
	        	echo json_encode($arr);
			}

    	}

	}
    //myset 密码
    public function pass()
    {
    	if (IS_POST) {

	    $userinfo = M('user')->where(array('id'=>$_SESSION['id']))->find();

		//当前密码
		if ($_POST['nowpass']!=$userinfo['password']) {
	        $arr = array(
				"error" => 0,
				"info" => "当前密码错误"
	        	);
	        echo json_encode($arr);
	        exit;
		}
		//新密码
		$preg_pass = "/^[0-9A-z]{6,16}$/";
		$pass_Yz = preg_match($preg_pass, $_POST["pass"]);
		if (!$pass_Yz) {
			$arr = array(
					"error" => 0,
					"info" => "密码必须是6~16位字符串"
				);
			echo json_encode($arr);
			exit;
		}
		//确认密码
		if ($_POST['pass']!=$_POST['repass']) {
			$arr = array(
					"error" => 0,
					"info" => "确认密码输入有误"
				);
			echo json_encode($arr);
			exit;
		}

		//更新密码
		M('user')->where(array('id'=>$_SESSION['id']))->setField('password',$_POST['pass']);

		//验证通过
		$arr = array(
			"error" => 1,
			"info" => "密码修改中"
        	);
        echo json_encode($arr);
      } 
       
    }

    //myset 原始头像上传
  //   public function img()
  //   {
		// //头像
		// if(!empty($_FILES['file']['tmp_name'])){
		// 	$upload = new \Think\Upload();// 实例化上传类    
		//     $upload->maxSize   =     3145728 ;// 设置附件上传大小    
		//     $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
		//     $upload->savePath  =      'Uploads/'; // 设置附件上传目录    
		//     // 上传单个文件     
		//     $info   =   $upload->uploadOne($_FILES['file']);    
		//     if(!$info) {// 上传错误提示错误信息        
		// 		$this->error($upload->getError());
		//     }else{
		//     // 上传成功 获取上传文件信息         
		//     	$face =  $info['savepath'].$info['savename'];   
		//     	//echo $face; 
		//     	//原图入库赋值
		// 		//$data['face'] = $face;
		// 		M('user')->where(array('email'=>$_SESSION['email']))->setField('face',$face);
		//     }
		// }
		// $this->redirect('Home/user/index');
  //   }
    //myset 美图秀秀接口头像处理上传
    public function img()
    {
    	//print_r($_POST);
	    $upload = new \Think\Upload();// 实例化上传类    
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小    
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
	    $upload->savePath  =      'Uploads/'; // 设置附件上传目录    
	    // 上传文件     
	    $info   =   $upload->upload();    
	    if(!$info) {// 上传错误提示错误信息        
	    	echo $upload->getError();    
	    }else{
	    // 上传成功        
	    	$info = current($info);//减维
	    	$face =  '/'.'Uploads/'.$info['savepath'].$info['savename']; 

	    	//头像新路径更新session
	    	$_SESSION['face'] = $face;

	    	//入库
	    	$res = M('user')->where(array('id'=>$_SESSION['id']))->setField('face',$face);
	    	if ($res) {
	    		echo $face;
	    	}else {
	    		echo '0';
	    	}
	    }

    }
    //邮箱绑定
    public function emai_binding()
    {
        $site_on_off = site_on_off();
        print_r($site_on_off);
    	//print_r($_POST);
    }


    //我的消息
    public function mymsg()
    {
        $sql = 'select t1.status,t1.caina_time,t2.title from answer t1 inner join question t2 on t1.qid=t2.id where t1.uid='.$_SESSION['id'].' and t1.status=1';
        $mymsg = M()->query($sql);
        $this->assign('mymsg',$mymsg);

        $this->display();
    }
    //消息删除

    //产品授权
    public function mylicense()
    {

        $this->display();
    }

    //激活邮件
    public function myemail()
    {

    	//登陆信息显示
        $this->_userinfo($_SESSION['id']);

        $this->display();
    }
    //
    public function send_email()
    {
    	$res = M('user')->where(array('id'=>$_SESSION['id']))->find();
    	//print_r($res);
    	//$str = '6666666666';
    	$url = 'http://'.$_SERVER['HTTP_HOST'].'/fly/index.php/home/user/form_activate?activate='.$res['openid'];
    	$str = <<<xiongge
    	<div><includetail><table border="0" cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555; font-family: 'Helvetica Neue Regular',Helvetica,Arial,Tahoma,'Microsoft YaHei','San Francisco','微软雅黑','Hiragino Sans GB',STHeitiSC-Light; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;"> <tbody style="margin: 0; padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;" href="http://fly.layui.com/" target="_blank">XXX社区 - 官方注册</a></div> </td> </tr> <tr style="margin: 0; padding: 0;"> <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight: 700;">{$res['username']}</em>，请完成以下操作： </p> <div style=""> <a href="{$url}" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank">立即激活邮箱</a> </div> <p style="line-height: 20px; margin-top: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"> <td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table></includetail></div>
xiongge;

    	$title = '激活邮件';
    	//发email
    	qq_mail($res['email'],$title,$str);


    }

    //激活
    public function form_activate()
    {
    	//print_r($_GET);
    	$activate = I('get.activate');
    	$res = M('user')->where(array('openid'=>$activate))->find();
    	//var_dump(strlen($res['openid']) );exit;
    	if ($res) {
    		//返回对比正常 修改状态
    		$data = M('user')->where(array('id'=>$res['id']))->setField('is_activate',1);
    		if ($data) {
    			//激活成功 QQ
    			if (strlen($res['openid'])== 32 ) {
    				//echo 1111;
    				$this->redirect('Home/login/qqlogin');
    			}else {  
    				//非QQ
    				//echo 2222;
    				$this->redirect('Home/login/index');
    			}    			
    		}else {
    			//激活失败
    			//echo 3333;
    			$this->redirect('Home/login/logout','',3,'激活失败');
    		}

    	}
    }

    //解除绑定 邮箱不能登陆 删除邮箱 激活状态取消
    public function remove_activate()
    {
    	//print_r($_GET['id']);
    	$id = I('get.id',0);
    	$res = M('user')->where(array('id'=>$id))->find();

    	//判断是否QQ  	
    	if (strlen($res['openid'])== 32 ) {
    		$data['email'] = '';
    		$data['is_activate'] = 0;
    		$res = M('user')->where(array('id'=>$id))->save($data);
    		if ($res) {
    			$this->success('解除绑定', U('home/user/myset'),3);
    		}else {
    			$this->error('解除失败', U('home/user/myset'),3);
    		}   		
    	}else {

    		//非QQ
    		$res = M('user')->where(array('id'=>$id))->delete();
    		$this->success('解除绑定,邮箱将不能登陆,正在销户...', U('Home/login/logout'),3);

    	}
    }




}


